/*
 * @Author: leo
 * @Date: 2024-02-26 13:45:15
 * @LastEditors: leo
 * @LastEditTime: 2024-02-27 10:44:02
 * @Description:
 */
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Session, VersioningType } from '@nestjs/common';
import * as session from 'express-session';
import { Request, Response, NextFunction } from 'express';
/* 引入 cors，解决跨域 */
import * as cors from 'cors';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';

import { GlobalResponse } from './common/response';

const whiteList = ['/list', '/user'];

/* 全局中间件 */
function MiddleWareAll(req: Request, res: Response, next: NextFunction) {
  console.log(req.originalUrl);
  if (whiteList.includes(req.originalUrl)) {
    next();
  } else {
    res.send({ message: '不在权限名单中' });
  }
}

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  app.useStaticAssets(join(__dirname, 'images'), {
    prefix: '/maomao',
  });
  /* 版本控制 */
  /** v1/user
   *  v2/user
   * */
  app.enableVersioning({
    type: VersioningType.URI,
  });
  app.use(cors());
  // app.use(MiddleWareAll);

  /* 使用 session 保存 cookie  */
  app.use(
    session({
      secret: 'wzy',
      rolling: true,
      name: 'wzy.sid',
      cookie: { maxAge: 99999999 },
    }),
  );
  /* 响应拦截器 */
  app.useGlobalInterceptors(new GlobalResponse());
  await app.listen(3000);
}
bootstrap();
